import * as cc from 'cc';

const { ccclass, property, menu } = cc._decorator;
// https://blog.csdn.net/weixin_44053279/article/details/138657518 分包的学习
@ccclass('Preload')
@menu('Scene/Preload')
export class Preload extends cc.Component {

    @property({ type: cc.Node })
    loading: cc.Node = null!

    onLoad(): void {
        console.log('onload', this.loading);
        const progress = this.loading.getComponent(cc.ProgressBar)
        if (progress) {
            // 批量加载资源
            cc.resources.loadDir('image/card_large', (finished: number, total: number) => {
                progress.progress = total / finished;
            }, (err, items) => {
                if (cc.assetManager.main)
                    cc.assetManager.main.loadScene('test', (err, scene) => {
                        cc.director.runScene(scene)
                    })
            })
        }
    }

    start(): void {
        // if (DEBUG) profiler.showStats();
        // // ECSModel.debugLog();

        // console.log(' launch scene', director.getScene())
        // // oops.gui.init(UIConfigData);
        // this.launch.on(NodeEventType.MOUSE_DOWN, () => {
        //     if (assetManager.main)
        //         assetManager.main.loadScene('test', (err, scene) => {
        //             director.runScene(scene)
        //         })
        // }, this);
    }

    // protected initGui(): void {
    //     super.initGui()
    //     console.log(' show initGui ');
    //     const button = this.gui.getChildByUuid('47sDthTNdMf66Mue7DfnMX');
    //     button.on(cc.NodeEventType.MOUSE_DOWN, this.listenerClick.bind(this))
    // }

    // protected onDestroy(): void {
    //     super.onDestroy()
    //     console.log(' show onDestroy ');
    //     const button = this.gui.getChildByUuid('47sDthTNdMf66Mue7DfnMX');
    //     button.off(cc.NodeEventType.MOUSE_DOWN, this.listenerClick.bind(this))
    // }

    listenerClick() {
        // console.log(' show assets ');
        // cc.assetManager.assets.forEach((v, idx) => {
        //     console.log(idx, v)
        // })
        // console.log(' show bundles ');
        // cc.assetManager.bundles.forEach((v, idx) => {
        //     console.log(idx, v)
        // })

        const scene = cc.director.getScene()

        // console.log(' assetManager.main.base', cc.assetManager.main.base);
        // console.log(' assetManager.main.deps', cc.assetManager.main.deps);
        if (cc.assetManager.main)
            cc.assetManager.main.loadScene('launch', (err, scene) => {
                cc.director.runScene(scene)
            })

        // console.log(' show scene ', this.game.getChildByName('Canvas'));

        // // 添加一个预制件
        // cc.resources.loadDir('image/card_large/card_clubs_04', (err, items) => {
        //     const texture = cc.resources.get('image/card_large/card_clubs_04', cc.ImageAsset);
        //     console.log(' preloadDir ', err, items, texture)
        //     cc.resources.load('prefab/Poker', (error, prefab) => {
        //         console.log(' pokerClass ', error, prefab)
        //         const poker = cc.instantiate(prefab as cc.Prefab);
        //         const spriteComp = poker.getComponent(cc.Sprite);
        //         const widgetComp = poker.getComponent(cc.Widget);
        //         spriteComp.spriteFrame = cc.SpriteFrame.createWithImage(texture);
        //         console.log(spriteComp, widgetComp);
        //         this.game.getChildByName('Canvas').addChild(poker);
        //         console.log(' poker ', poker)
        //     })
        // })
    }

    // // 添加一个预制件
    // cc.resources.loadDir('image/card_large/card_clubs_04', (err, items) => {
    //     const texture = cc.resources.get('image/card_large/card_clubs_04', cc.ImageAsset);
    //     console.log(' preloadDir ', err, items, texture)
    //     cc.resources.load('prefab/Poker', (error, prefab) => {
    //         console.log(' pokerClass ', error, prefab)
    //         const poker = cc.instantiate(prefab as cc.Prefab);
    //         const spriteComp = poker.getComponent(cc.Sprite);
    //         const widgetComp = poker.getComponent(cc.Widget);
    //         spriteComp.spriteFrame = cc.SpriteFrame.createWithImage(texture);
    //         console.log(spriteComp, widgetComp);
    //         this.game.getChildByName('Canvas').addChild(poker);
    //         console.log(' poker ', poker)
    //     })
    // })

}

